﻿using Microsoft.Extensions.DependencyInjection;
using SSPivot.Common.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.AspNetCore.Abstractions.Data
{
    public class DataSeeder : IDataSeeder, ITransientDependency
    {
        protected IServiceScopeFactory ServiceScopeFactory { get; }

        public DataSeeder(IServiceScopeFactory serviceScopeFactory)
        {
            ServiceScopeFactory = serviceScopeFactory;
        }
        public virtual async Task SeedAsync()
        {
            using (var scope = ServiceScopeFactory.CreateScope())
            {
                var contributors = scope.ServiceProvider.GetServices<IDataSeedContributor>();

                foreach (var contributor in contributors)
                {
                    await contributor.SeedAsync();
                }
            }
        }
    }
}
